package user

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	loginRpc "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员管理 - 会员管理 - 会员详情操作: 更新会员状态
func NewUpdateUserStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserStatusLogic {
	return &UpdateUserStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserStatusLogic) UpdateUserStatus(req *types.UpdateUserStatusRequest) (resp *types.UpdateUserStatusResponse, err error) {
	var status int32
	if req.Status {
		status = 1
		// 踢下线
		_, err := l.svcCtx.LoginRpc.Logout(l.ctx, &loginRpc.LogoutRequest{
			UserId:   int64(req.UserID),
			Source:   headInfo.GetSource(l.ctx),
			Business: headInfo.GetBusiness(l.ctx),
		})
		if err != nil {
			logc.Error(l.ctx, "logout err:", err)
		}
	} else {
		status = 0
	}
	_, err = l.svcCtx.UserServiceRPC.UserModifyStatus(l.ctx, &userrpc.UserModifyStatusRequest{
		UserId: int64(req.UserID),
		Status: status,
	})
	return
}
